/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.TaxClassModel;
import com.elf.gwt.common.client.model.TaxRateModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class TaxRatesComponent extends EditTableComponent
{	
	protected static TaxClassesComponent taxClassesComponent = null;
	protected List<TaxRateModel> taxRateModels;
	protected TaxRateModel emptyTaxRateModel;

	/**
	 * Constructor
	 */
	public TaxRatesComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		emptyTaxRateModel = new TaxRateModel();
		taxRateModels = new ArrayList<TaxRateModel>();
		paginate = true;
		tableType = "tax rates";
		if (taxClassesComponent == null) taxClassesComponent = (TaxClassesComponent)controller.getComponent("TaxClassesComponent");
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		taxRateModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		TaxRateModel model = new TaxRateModel(jsonObject);
		taxRateModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = taxRateModels.size();
		if (edit)
			numberOfTableColumns = 6;
		else 
			numberOfTableColumns = 5;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Priority");
		grid.setText(0, 1, "Tax Class");
		grid.setText(0, 2, "Tax Zone");
		grid.setText(0, 3, "Tax Rate");
		grid.setText(0, 4, "Description");
		if (edit) grid.setText(0, 5, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, taxRateModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyTaxRateModel);
	}

	public void displayRow(Grid grid, int row, TaxRateModel model) {
		List<TaxClassModel> taxClassModels = taxClassesComponent.getTaxClassModels();
		if (edit) {
			Hidden idHidden = new Hidden();
			idHidden.setName("taxRateId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("taxRateId-" + row, idHidden);

			TextBox priorityTextBox = new TextBox();
			priorityTextBox.setWidth("80px");
			priorityTextBox.setText(String.valueOf(model.getPriority()));
			elements.put("taxRatePriority-" + row, priorityTextBox);
			grid.setWidget(row, 0, priorityTextBox);

			int i = 0;
			ListBox taxClassListBox = new ListBox();
			taxClassListBox.addItem("None", "0");
			if (0 == model.getTaxClassId())
			{
				taxClassListBox.setSelectedIndex(i);
			}
			i++;
			Iterator<TaxClassModel> iter1 = taxClassModels.iterator();
			while(iter1.hasNext())
			{
				TaxClassModel tcmodel = iter1.next();
				taxClassListBox.addItem(tcmodel.getTitle(), String.valueOf(tcmodel.getId()));
				if (tcmodel.getId() == model.getTaxClassId())
				{
					taxClassListBox.setSelectedIndex(i);
				}
				i++;
			}
			elements.put("taxRateClassId", taxClassListBox);
			grid.setWidget(row, 1, taxClassListBox);

			TextBox rateTextBox = new TextBox();
			rateTextBox.setWidth("80px");
			rateTextBox.setText(String.valueOf(model.getRate()));
			elements.put("taxRate-" + row, rateTextBox);
			grid.setWidget(row, 3, rateTextBox);

			TextBox descTextBox = new TextBox();
			descTextBox.setWidth("400px");
			descTextBox.setText(model.getDescription());
			elements.put("taxRateDescription-" + row, descTextBox);
			grid.setWidget(row, 4, descTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("taxRateDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 5, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, String.valueOf(model.getPriority()));

			int i = 0;
			String taxClassText = "None";
			i++;
			Iterator<TaxClassModel> iter1 = taxClassModels.iterator();
			while(iter1.hasNext())
			{
				TaxClassModel tcmodel = iter1.next();
				if (tcmodel.getId() == model.getTaxClassId())
				{
					taxClassText = tcmodel.getTitle();
				}
				i++;
			}
			grid.setText(row, 1, taxClassText);
			
			grid.setText(row, 3, String.valueOf(model.getRate()));
			grid.setText(row, 4, model.getDescription());
		}
	}

	public List<TaxRateModel> getTaxRateModels() {
		return taxRateModels;
	}
}
